package com.ukao.cashregister.dialog;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.ukao.cashregister.R;
import com.ukao.cashregister.base.BaseDialogFragment;
import com.ukao.cashregister.eventbus.UserInfoEvent;
import com.ukao.cashregister.listener.OnDialogSureClickListener;
import com.ukao.cashregister.utils.CheckUtils;
import com.ukao.cashregister.utils.SystemUIUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnCheckedChanged;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 选择性别
 * Created by ukaodev on 2017/6/2.
 */

public class EditSexDialogFragment extends BaseDialogFragment {

    Unbinder unbinder;
    @BindView(R.id.edit_sex_radioGroup)
    RadioGroup mRGroup;
    @BindView(R.id.edit_sex_male_tab)
    RadioButton mMaleTab;
    @BindView(R.id.edit_sex_female_tab)
    RadioButton mFemaleTab;
    @BindView(R.id.edit_sex_cancel)
    Button mSexCancel;
    @BindView(R.id.edit_sex_submit)
    Button mSexSubmit;
    @BindView(R.id.title)
    TextView couponsTitle;
    @BindView(R.id.unknown)
    RadioButton unknown;
    private String sex;

    private OnDialogSureClickListener mOnDialogSureClickListener;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_fragment_edit_sex, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (getDialog()!=null){
            SystemUIUtils.setStickFullScreen(getDialog().getWindow());
        }
        Bundle bundle = getArguments();
        couponsTitle.setText("修改性別");
        sex = bundle.getString("sex", "");
        if(sex.equals("男")) {
            mRGroup.check(R.id.edit_sex_male_tab);
        } else if (sex.equals("女")) {
            mRGroup.check(R.id.edit_sex_female_tab);
        } else if (sex.isEmpty()||sex.equals("未知")){
            mRGroup.check(R.id.unknown);
        }

    }




    public void setOnSureClickListener(OnDialogSureClickListener mOnDialogSureClickListener) {
        this.mOnDialogSureClickListener = mOnDialogSureClickListener;
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }




    @OnClick({R.id.edit_sex_cancel, R.id.edit_sex_submit,R.id.close_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.edit_sex_cancel:
                dismiss();
                break;
            case R.id.close_btn:
                dismiss();
                break;

            case R.id.edit_sex_submit:
                int checkid = mRGroup.getCheckedRadioButtonId();
                if (CheckUtils.isNull(checkid))return;
                String sex = ((RadioButton) mRGroup.findViewById(mRGroup.getCheckedRadioButtonId())).getText().toString();
                if(mOnDialogSureClickListener !=null)
                    mOnDialogSureClickListener.onSureItemClick(sex);
                else UserInfoEvent.postHasData(UserInfoEvent.Message.SEX, sex);
                dismiss();
                break;
        }
    }

    @OnCheckedChanged({R.id.edit_sex_male_tab, R.id.edit_sex_female_tab})
    public void OnCheckedChanged(CompoundButton view, boolean checked) {
        if (checked) {
            switch (view.getId()) {
                case R.id.edit_sex_male_tab:
                    break;
                case R.id.edit_sex_female_tab:
                    break;
            }
        }
    }
}
